package calc;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Calculator extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 323453L;
	private JTextField monitor; // 用来显示输入和计算结果
	private boolean isNew = true;
	private double result = 0;
	private String preop = "+";
	private boolean multiop=false;
	private JButton but;
    private boolean numbool=false;
    
	public Calculator() {
		setTitle("计算器");
		setSize(305, 410);
		setVisible(true);
		setLocation(200, 300);

		Font font = new Font("微软雅黑", Font.TRUETYPE_FONT, 18);

		JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		add(pane, BorderLayout.NORTH);
		monitor = new JTextField("0");
		monitor.setHorizontalAlignment(JTextField.RIGHT);
		monitor.setEnabled(false);
		monitor.setDisabledTextColor(new Color(0x1874CD));
		monitor.setFont(font);
		monitor.setBorder(BorderFactory.createLoweredBevelBorder());
		pane.add(monitor, BorderLayout.CENTER);
		but = new JButton("清除");
		but.addActionListener(this);
		but.setBackground(new Color(0xC5C1AA));
		but.setFont(font);
		/*
		 * but.addActionListener(new ActionListener() {
		 * 
		 * @Override public void actionPerformed(ActionEvent e) {
		 * monitor.setText(""); } });
		 */
		pane.add(but, BorderLayout.EAST);

		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(4, 4));
		add(panel1, BorderLayout.CENTER);

		String[] str = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/" };
		for (int i = 0; i < str.length; i++) {
			but = new JButton(str[i]);
			but.setBackground(new Color(0xE0E0E0));
			but.setBorder(BorderFactory.createLoweredBevelBorder());
			but.setFont(font);
			but.addActionListener(this);
			panel1.add(but);
		}

		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new Calculator();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String btCom = e.getActionCommand();
		if (btCom.equals("清除")) {
			monitor.setText("0");
			result=0;
			preop="+";
			isNew=true;
			multiop=false;
			numbool=false;
		} else if ("0123456789.".indexOf(btCom) >= 0) {
			// 数字
			showNum(btCom);
		} else {
			// 运算符
			compute(btCom);
		}
	}

	private void compute(String btCom) {
		isNew = true;
		String str1=monitor.getText();
		if(multiop){preop=btCom;return;}
		if(numbool) {monitor.setText("");return;}
		if(preop.equals("/")&&str1.equals("0")) {
			monitor.setText("");
			numbool=true;
			return;
		}
		Double num = Double.valueOf(monitor.getText());
		BigDecimal bd=new BigDecimal(result);
		
		switch (preop) {
		case "+":
			result+=num;
			break;
		case "-":
			bd=bd.subtract(new BigDecimal(num));	
			result=bd.doubleValue();
			break;
		case "*":
			result*=num;
			break;
		case "/":
			result/=num;
			break;
		}
		String str=String.valueOf(result);
		if(str.endsWith(".0")) str=str.substring(0, str.length()-2);
		monitor.setText(str);
		preop = btCom;
		multiop=true;
	}

	private void showNum(String btCom) {
		multiop=false;
		String yuan = monitor.getText();
		if(numbool) {monitor.setText("");return;}
		if(preop.equals("/")&&yuan.equals("0")&&btCom!=".") {
			monitor.setText("");
			numbool=true;
			return;
		}
		if (isNew) {
			yuan = "0";
			isNew = false;
		}
		if (yuan.equals("0")) {
			if (btCom.equals("."))
				monitor.setText("0.");
			else
				monitor.setText(btCom);
			return;
		}
		if (yuan.indexOf(".") >= 0 && btCom.equals("."))
			return;
		monitor.setText(yuan + btCom);
	}
}
